﻿using System;
using System.Xml;

namespace Useful.Xml
{
    public class XmlHelper
    {
        public static XmlNode CreateXmlNode(XmlDocument doc, string name)
        {
            return doc.CreateNode(XmlNodeType.Element, name, null);
        }

        public static XmlNode CreateXmlNodeDotReplace(XmlDocument doc, string name, object value)
        {
            XmlNode xml = CreateXmlNode(doc, name);

            if (value != null)
                xml.InnerText = value.ToString().Replace(',', '.');

            return xml;
        }

        public static XmlNode CreateXmlNode(XmlDocument doc, string name, object value)
        {
            XmlNode xml = CreateXmlNode(doc, name);

            if (value != null)
                xml.InnerText = value.ToString();

            return xml;
        }

        public static XmlNode CreateXmlNode2(XmlDocument doc, string name, object value)
        {
            XmlNode xml = CreateXmlNode(doc, name);

            xml.InnerText = value != null ? value.ToString() : null;

            return xml;
        }

        public static void AppendChildDotReplace(XmlNode parent, XmlDocument doc, string name, object value)
        {
            if (value != null && value != DBNull.Value && value.ToString() != String.Empty)
                parent.AppendChild(CreateXmlNodeDotReplace(doc, name, value));
        }

        public static void AppendChild(XmlNode parent, XmlNode child)
        {
            parent.AppendChild(child);
        }

        public static void AppendChild(XmlNode parent, XmlDocument doc, string name, object value)
        {
            if (value != null && value != DBNull.Value && value.ToString() != String.Empty)
                parent.AppendChild(CreateXmlNode(doc, name, value));
        }

        public static void AppendChild2(XmlNode parent, XmlDocument doc, string name, object value)
        {
            parent.AppendChild(CreateXmlNode2(doc, name, value));
        }
    }
}